package com.heimali.happyreader.http;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.heimali.happyreader.config.Constants;
import com.heimali.happyreader.model.Article;
import com.heimali.happyreader.model.Intro;
import com.heimali.happyreader.model.LoginUserInfo;
import com.heimali.happyreader.model.UpdateInfo;
import com.heimali.happyreader.utils.SystemUtils;

public class JsonParser {
	private static final String TAG = "JsonParse";

	public static ArrayList<Intro> listParser(String str) {
		if (TextUtils.isEmpty(str)) {
			return null;
		}

		ArrayList<Intro> intros = new ArrayList<Intro>();
		try {
			JSONObject res = new JSONObject(str);
			String errno = res.getString("errno");
			if ("0".equals(errno)) {
				JSONArray array = res.getJSONArray("data");
				for (int i = 0, len = array.length(); i < len; i++) {
					JSONObject obj = (JSONObject) array.opt(i);
					Intro item = new Intro(obj.getInt("id"), obj.getString("time"), obj.getString("title"),
							obj.getString("intro"));
					intros.add(item);
				}
			} else {
				SystemUtils.Log(TAG, errno, res.getString("errmsg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return intros;
	}

	public static Article articleParser(String str) {
		if (TextUtils.isEmpty(str)) {
			return null;
		}

		Article article = null;

		try {
			JSONObject res = new JSONObject(str);
			String errno = res.getString("errno");
			if ("0".equals(errno)) {
				JSONObject data = res.getJSONObject("data");
				article = new Article(data.getInt("id"), data.getString("surl"), data.getString("title"),
						data.getString("content"));
			} else {
				article = new Article(Constants.E_SERVER_BUSY, null);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			SystemUtils.Log(e.getMessage());
		}

		return article;
	}

	public static LoginUserInfo loginInfoParser(String str) {
		if (TextUtils.isEmpty(str)) {
			return null;
		}

		LoginUserInfo info = null;

		try {
			JSONObject res = new JSONObject(str);
			int suc = res.getInt("suc");
			if (suc == 1) {
				info = new LoginUserInfo(suc, res.getString("errmsg"), res.getString("token"), res.getString("uid"),
						res.getString("name"));
			} else {
				info = new LoginUserInfo(suc, res.getString("errmsg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
			SystemUtils.Log(e.getMessage());
		}

		return info;
	}

	public static UpdateInfo updateInfoParser(String str) {
		if (TextUtils.isEmpty(str)) {
			return null;
		}

		UpdateInfo info = null;
		try {
			JSONObject obj = new JSONObject(str);
			info = new UpdateInfo(obj.getInt("minVerCode"), obj.getInt("verCode"), obj.getString("verName"),
					obj.getString("description"), obj.getString("md5"), obj.getString("url"));

		} catch (JSONException e) {
			e.printStackTrace();
			SystemUtils.Log(e.getMessage());
		}

		return info;
	}
}
